package co.edu.javeriana.monopoly.client.gui;

import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.plaf.basic.BasicLabelUI;

class RotateLabelUI extends BasicLabelUI {
	private int grade;
	
	static {
		labelUI = new RotateLabelUI ( 0 );
	}
	
	RotateLabelUI( ) {
		super ( );
		setGrade ( 0 );
	}
	
	RotateLabelUI( int grade ) {
		super ( );
		setGrade ( grade );
	}
	

    /*public Dimension getPreferredSize ( JComponent c ) {
    	Dimension dim = c.getPreferredSize ( );
    	double rad = Math.PI / 180 * grade;
//    	System.out.println ( "pos" + c.getY() + " label x: " + dim.getWidth() + " y: " + dim.getHeight() + " rad : " + rad + "atan :" + Math.cos(rad + Math.atan ( (float)dim.getHeight ( ) / dim.getWidth ( ) ) ) );
    	double x = dim.getWidth ( ), y = dim.getHeight ( );
    	//if ( dim.getWidth ( ) != 0.0 ) {
        //    x = (int)Math.abs ( Math.cos ( rad + Math.atan ( (float)dim.getHeight ( ) / dim.getWidth ( ) ) ) );
        //    y = (int)Math.abs ( Math.sin ( rad + Math.atan ( (float)dim.getHeight ( ) / dim.getWidth ( ) ) ) );
    	//}
    	if( grade == 90 || grade == 270 ) {
    		x = dim.getHeight ( );
    		y = dim.getWidth ( );
    		System.out.println("en el if");
    	}
    	System.out.println ( ((JLabel)c).getText ( ) + "x: " + x + " y: " + y );
    	return new Dimension ( (int)x, (int)y );
    }*/	

    private static Rectangle paintIconR = new Rectangle ( );
    private static Rectangle paintTextR = new Rectangle ( );
    private static Rectangle paintViewR = new Rectangle ( );
    private static Insets paintViewInsets = new Insets (0, 0, 0, 0 );

	public void paint ( Graphics g, JComponent c ) {
        JLabel label = (JLabel)c;
        String text = label.getText ( );
        Icon icon = label.isEnabled ( ) ? label.getIcon ( ) : label.getDisabledIcon ( );

        if ( icon == null && text == null ) 
            return;
    	double rad = ( Math.PI / 180 ) * grade;

        FontMetrics fm = g.getFontMetrics ( );
        paintViewInsets = c.getInsets ( paintViewInsets );

        paintViewR.x = paintViewInsets.left;
        paintViewR.y = paintViewInsets.top;
    	
    	// Use inverted height & width
        if ( grade == 90 || grade == 270  ){ 
            paintViewR.height = c.getWidth ( ) - ( paintViewInsets.left + paintViewInsets.right );
            paintViewR.width = c.getHeight ( ) - ( paintViewInsets.top + paintViewInsets.bottom );
        }
        else {
            paintViewR.width = c.getWidth ( ) - ( paintViewInsets.left + paintViewInsets.right );
            paintViewR.height = c.getHeight ( ) - ( paintViewInsets.top + paintViewInsets.bottom );
        }
        
        paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
        paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;

        String clippedText = layoutCL ( label, fm, text, icon, paintViewR, paintIconR, paintTextR );

    	Graphics2D g2 = (Graphics2D) g;
    	AffineTransform tr = g2.getTransform ( );
    	g2.rotate ( rad );
  	
    	if( grade == 90 ) 
    		g2.translate ( 0, - c.getWidth ( ) );
    	else if ( grade == 270 )
    		g2.translate ( - c.getHeight ( ), 0 );
    	else if ( grade == 180 )
    		g2.translate ( -c.getWidth ( ), -c.getHeight ( ) );
    	
    	if ( icon != null ) 
            icon.paintIcon ( c, g, paintIconR.x, paintIconR.y );

        if ( text != null ) {
            int textX = paintTextR.x;
            int textY = paintTextR.y + fm.getAscent ( );

            if ( label.isEnabled ( ) ) 
                paintEnabledText( label, g, clippedText, textX, textY );
            else 
                paintDisabledText ( label, g, clippedText, textX, textY );
        }

    	g2.setTransform ( tr );
    }


	public int getGrade ( ) {
		return grade;
	}


	public void setGrade ( int grade ) {
		this.grade = grade;
	}
}

